#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash.h"

/*this function writes input to the buffer param--
 * clearifies the main
 */
void readBuffer(char *buff){
	int c = 0;
	int i = 0;

	while ((c = getchar()) != '\n'){
		*(buff + i++) = c;
	}
	*(buff + i) = '\0';
}



int main(void)
{
	my_list Data_table[HASH_TABLE_SIZE];
	unsigned id = 0;
	char buffer[100];
	char *toker;
	char *name;
	int retVal = 0;

	DS_init(Data_table);						/*initializing the table*/

	while (strcmp(buffer,"QUIT") != 0){
		readBuffer(buffer);
		toker = strtok(buffer, " ");

		if (strcmp(toker,"INSERT") == 0){
			toker = strtok(NULL, " ");
			id =(unsigned) atoi(toker);
			name = strtok(NULL, "?");
			if ((retVal = DS_insert(id, name, Data_table)) == 1) printf("Insertion succeeded.\n");
			else if (retVal == -7) {
				printf("mem. allocation failure!\n");
				break;
			}
			else printf("%u is already in the database.\n", id);
		}

		else if (strcmp(toker,"DELETE") == 0){
			toker = strtok(NULL, " ");
			id =(unsigned) atoi(toker);
			if (DS_delete(id, Data_table))	printf("Delete succeeded.\n");
			else printf("%u is not in the database.\n", id);
		}

		else if(strcmp(toker,"GETNAME") == 0){
			toker = strtok(NULL, " ");
			id =(unsigned) atoi(toker);
			name = DS_get_record(id, Data_table);
			if (name == '\0') printf("%u is not in the database.\n", id);
			else printf("%s\n", name);
		}
	}
	DS_free(Data_table); 						/*before exiting the prog. - free the mem. used*/
	return 0; 
}
